package com.gewu.pm.ui.fragment.terminal;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.bean.PromotionBean;
import com.gewu.pm.ui.activity.filter.FilterMultipleActivity;
import com.gewu.pm.ui.activity.terminal.EventDetailsActivity;
import com.gewu.pm.ui.adapter.terminal.TerminalPromotionAdapter;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 终端详情 - 促销活动 Fragment
 */
public final class PromotionFragment extends AppFragment<AppActivity>
        implements BaseAdapter.OnItemClickListener {

    private TextView mFilterView;
    private TextView mDateRangeView;
    private RecyclerView mRecyclerView;
    private TerminalPromotionAdapter mAdapter;

    public static PromotionFragment newInstance() {
        return new PromotionFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.terminal_promotion_fragment;
    }

    @Override
    protected void initView() {
        mFilterView = findViewById(R.id.tv_filter);
        mDateRangeView = findViewById(R.id.tv_date_range);
        mRecyclerView = findViewById(R.id.rv_promotion_list);

        // 设置 RecyclerView
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new TerminalPromotionAdapter(getContext());
        
        // 必须在 setAdapter 之前设置监听器
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);

        setOnClickListener(R.id.ll_date_filter, R.id.tv_date_range);

    }

    @Override
    protected void initData() {
        mFilterView.setText("全部");
        // TODO: 加载促销活动数据
        List<PromotionBean> testData = new ArrayList<>();
        testData.add(new PromotionBean("买赠活动",1));
        testData.add(new PromotionBean("满减活动",2));
        testData.add(new PromotionBean("优惠卷活动",3));
        mAdapter.setData(testData);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        if (id == R.id.ll_date_filter) {//筛选
            // TODO: 打开筛选
            FilterMultipleActivity.start(getActivity());
        } else if (id == R.id.tv_date_range){
            // TODO: 打开日期选择器
            toast("选择日期范围");
        }
    }

    @Override
    public void onItemClick(androidx.recyclerview.widget.RecyclerView recyclerView, android.view.View itemView, int position) {
        PromotionBean bean = mAdapter.getItem(position);
        // TODO: 打开促销活动详情
        EventDetailsActivity.start(getAttachActivity(), bean.getType());
    }
}

